<?php htmlTemplate('index/header.php')?>

		<div class="nav">			
			<span class="nav_span">
			<a class="nav_home" href="<?php echo MVC_WWW_HOST;?>">&nbsp;</a>
			<a href="<?php echo MVC_WWW_HOST?>library/index">Library</a> :: <?php if (@$row){
				echo 'Set Question';
			}else {
				echo 'Add Question';
			}?>
			</span>
		</div>
		<div class="common_search_wrap" style="text-align:left">
		Select Question Type:
		<select name="type" id="type" onchange="changeType()">
		    <?php if (@$row){
		    	echo '<option value="'.$row['type'].'">'.$row['name'].'</option>';
		    }?>
			<?php foreach ($types as $k=>$v):?>
			<option value="<?php echo $v['id']?>"><?php echo $v['name']?></option>
			<?php endforeach;?>
			</select>
		</div>
		<div class="lib_add_wrap">
			<table class="com_table" cellspacing="1" cellpadding="3" >			  
			  <tr>
			    <td style="width:150px;">&nbsp;Set Question:</td>
			    <td valign="top" style="position: relative;">
			    <textarea rows="3" cols="100" id="question"><?php			   
			     if (@$row) echo $row['question'];
			     ?></textarea> <a href="javascript:;" onclick="addblank()" class="addblank" id="addblank" style="text-decoration: underline;display: none;color:red">&lsaquo;&lsaquo; Add Blank</a>
			    </td>			    
			  </tr>			  
			  <tr>
			    <td>&nbsp;Question to cate:</td>
			    <td >
			    	<select name="cate" id="cate" >	
			    	<?php if (@$row){
			    		if ($row['cate']){
				    		echo '<option value="'.$row['cate'].'">'.$row['catename'].'</option>';
			    		}
				    }?>		    		
			    		<?php
			    		if ($cates){
				    		foreach ($cates as $k=>$v): 
				    		if (@$row){
				    			if ($v['id']==$row['cate']) continue;
				    		}
				    		?>
				    		<option value="<?php echo $v['id']?>"><?php echo $v['name']?></option>
				    		<?php 
				    		endforeach;
				    		echo '<option value="0">默认分类</option>';
			    		}else {
			    			echo '<option value="0">默认分类</option>';
			    		}?>
			    	</select>
			    	<input type="button" class="add_btn" onclick="addCate('addcate')" />
			    	<span id="addcate" style="display:none;">
			    	<input type="text" class="input3" id="savecate"> <input type="button" class="search_submit2" onclick="saveCate()" value="Save">
			    	</span>
			    </td>			   
			  </tr>
			  <tr class="com_table_tr3">
			    <td colspan="2">&nbsp;Set Answer:</td>
			   	    
			  </tr>
			 
			  <tr>
			    <td colspan="2"  class="com_tbd" id="answers">
                  
                </td>
			        
			  </tr>
				  
			
			  <tr class="com_table_tr2">
			  	<td>&nbsp;</td>
			  	<td><input type="button" value="Save" class="search_submit" onclick="saveAnswer()"></td>
			  </tr>
			</table>
			
		</div>

<script type="text/javascript">
function addblank()
{
	//var inval = $("#question").val();
	//$("#question").val(inval+' ______ ');
	$('#question').insertRoundCaret('space')
	
}
function changeType()
{
	var type = $("#type").val();
	$("#addblank").hide();
	
	<?php if (@$row){?>
	var url = '<?php echo MVC_WWW_HOST;?>library/answer/catehtml/'+type+'/id/<?php echo $row['id'];?>';	
	<?php }else {?>
	var url = '<?php echo MVC_WWW_HOST;?>library/answer/catehtml/'+type;
	<?php }?>
	getHtml(url,'answers');
	bnums=0;
	<?php if (!@$row){?>
	$("#question").val('');
	<?php }?>
}
function saveAnswer()
{
	var type = $("#type").val();
	var cate = $("#cate").val();
	var question = $("#question").val();
	var answer = getAnswer();
	if(!answer){
		return false;
	}
	url = '<?php echo MVC_WWW_HOST;?>library/add<?php if(@$row) echo '/do/update/id/'.$row['id'];?>';
	$.post(url,{'type':type,'cate':cate,'question':question,'answer':answer},function(rs){
		alert(rs);
	})	
}
function addCate(id){
	$("#"+id).fadeIn();
}
function saveCate(){
	var savecate = $.trim($("#savecate").val());
	url = '<?php echo MVC_WWW_HOST;?>library/addcate.html';
	$.getJSON(url,{'savecate':savecate},function(data){
		alert(data.html);
		if(data.rs==1){
			var row = '<option value="'+ data.id +'">' +savecate+ '</option>';
			var tmpHtml = $("#cate").html();
			$("#cate").html(row+tmpHtml);
		}
		
	})
}
changeType();
</script>		
<?php htmlTemplate('index/footer.php')?>
